﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Net.Sockets;
using System.Net;
using System.Collections;


namespace $safeprojectname$
{
    public partial class Messanger : Form
    {
        byte[] data;
        Socket client;
        int recv;
        EndPoint remoteEP;
        Thread thread1;
        IPEndPoint localEP;
        ArrayList list;
        Socket server;
        public Messanger()
        {
            InitializeComponent();
            tbsendmsg.Enabled = true;
        }
        public Messanger(EndPoint _remoteEP)
        {
            remoteEP = _remoteEP;
            client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            client.SendTo(Encoding.ASCII.GetBytes("join"), remoteEP);
            data = new byte[1024];
            thread1 = new Thread(new ThreadStart(Rec));
            thread1.Start();
            this.Visible = false;
        }

        public void Rec()
        {
            while (true)
            {
                recv = client.ReceiveFrom(data,ref remoteEP);
                tbview.Text += Encoding.ASCII.GetString(data, 0, recv) + "\r\n";
            }
        }

        private void clientConnectionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Client c = new Client();
            c.Visible = true;
        }

        private void stopConnectionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            server.Close();
        }

        private void serverConnectionToolStripMenuItem_Click(object sender, EventArgs e)
        {   
            localEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9060);
            list = new ArrayList(10);
            server = new Socket(AddressFamily.InterNetwork,SocketType.Dgram, ProtocolType.Udp);

            server.Bind(localEP);
            EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);

            int recv;
            byte[] data;
            tbview.Text = "Waiting for a client...\n";
            /*
            while (true)
            {
                data = new byte[1024];
                recv = 0;
                recv = server.ReceiveFrom(data, ref remoteEP);
                

                String message = Encoding.ASCII.GetString(data, 0, recv);
                switch (message)
                {
                    case "join":
                        tbview.Text = "Received from"+ remoteEP.ToString() +": /n";                   
                        list.Add(remoteEP);
                        break;
                    case "quit":
                        list.Remove(remoteEP);
                        break;
                    default:
                        int i = 0;
                        String Data = Encoding.ASCII.GetString(data);
                        while (i < list.Count)
                        {
                            server.SendTo(Encoding.ASCII.GetBytes(Data), recv, SocketFlags.None, (EndPoint)list[i]);
                            i++;
                        }
                        break;
                }
            }*/
        }

        private void btnsend_Click(object sender, EventArgs e)
        {
            if (tbsendmsg.Text != "")
            {
                tbview.Text += tbsendmsg.Text + "\n";
                tbsendmsg.Text = "";
            }
        }
    }
}
